package cn.hhchat.wp.security.service;

import cn.hhchat.wp.entity.RoleEntity;
import cn.hhchat.wp.entity.UserEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.ArrayList;
import java.util.List;

final class JwtUserFactory {

    private JwtUserFactory() {
    }

    static JwtUser create(UserEntity user) {
        return new JwtUser(
                user.getId(),
                user.getUsername(),
                user.getEmail(),
                user.getPassword(),
                mapToGrantedAuthorities(user.getRoles()),
                user.getLastPwdChangeDate()
        );
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(List<RoleEntity> roleEntities) {
        List<GrantedAuthority> authorityList = new ArrayList<>();
        for (RoleEntity r : roleEntities) {
            authorityList.add(new SimpleGrantedAuthority(r.getName()));
        }
        return authorityList;
    }
}
